﻿using System;
using NL.ADA.ConfigService.DAL.EntityClasses;
using NL.ADA.ConfigProxy;
using NL.ADA.ConfigClient;
using NUnit.Framework;
using NL.ADA.Common;
using System.Threading;

namespace NUnit
{
    [TestFixture]
    public class UnitTestProxy
    {

        [Test]
        public void GetStorageFromServer_Test()
        {
            Proxy proxy = Proxy.Instance;
            ConfigTemplate template = proxy.GetServerConfigTemplate("TestApplication-2008", "DEV");
            Assert.That(template.Config.Count > 0);

        }

        [Test]
        public void StoreLocalConfigTemplate_Test()
        {
            Proxy proxy = Proxy.Instance;
            ConfigTemplate template = proxy.GetServerConfigTemplate("TestApplication-2008", "DEV");
            var result = proxy.StoreLocalConfigTemplate(template);
            Assert.That(result == true);
        }

        [Test]
        public void DeleteLocalConfigTemplate_Test()
        {
            Proxy proxy = Proxy.Instance;
            proxy.DeleteLocalConfigTemplateFile("adaconfig.ada");
        }

        [Test]
        public void GetLocalConfigTemplate_Test()
        {
            bool exceptionThrown = false;
            Proxy proxy = Proxy.Instance;
            try
            {
                ConfigTemplate template = proxy.GetLocalConfigTemplate("adaconfig.ada");
                //should fail because of previous --delete-- test

            }
            catch (Exception ex)
            {
                exceptionThrown = true;
            }
            finally
            { Assert.That(exceptionThrown == true); }

        }

        [Test]
        public void GetServerModified_Test()
        {
            Gateway gateway = new Gateway();
            DateTime modified = gateway.GetServerModified(2, 9);

        }
        [Test]
        public void UpdateConfig_Test()
        {
            ConfigManager mgr = new ConfigManager();
            var entity = new ConfigEntity();
            entity.ApplicationId = 2;
            entity.ModeId = 9;
            entity.Key = "hello2";
            entity.Value = "Test";
            entity.ConfigType = 1;
            var result = mgr.Update(entity);
            Assert.That(result == true);

        }
        [Test]
        public void GetConfigValue_test()
        {
            Proxy proxy = Proxy.Instance;
            string result = ConfigurationManager<string>.AppSettings["hello"];
            //check whether get the setting properly through the singleton instance:
            string result2 = ConfigurationManager<string>.AppSettings["hello"];
            Assert.That(result2 != String.Empty && result != String.Empty);
        }

        [Test]
        public void CheckServerModified_Test()
        {
            Proxy proxy = Proxy.Instance;
            var result = proxy.ServerModified();
            Assert.That(result == true);
        }

        [Test]
        public void RegisterNotification_Test()
        {
            var result = false;
            try
            {
                var proxy = Proxy.Instance;
                result = true;

            }
            finally
            {
                Assert.IsTrue(result);
            }

        }
        [Test]
        public void UnRegisterNotification_Test()
        {
            var result = false;
            try
            {
                var proxy = Proxy.Instance;
                proxy.Dispose();
                result = true;
            }
            finally
            {
                Assert.IsTrue(result);
            }

        }

        [Test]
        public void DisconnectProxy_Test()
        {
            var result = false;
            try
            {
                string s = ConfigurationManager.AppSettings["test"];
                ConfigurationManager.Disconnect();
                result = true;
            }
            finally
            {
                Assert.IsTrue(result);
            }

        }

        [Test]
        public void FirstInitializeProxy_Test()
        {
            //prepare for first connection:
            DeleteLocalConfigTemplate_Test();
            //first connection:
            Proxy proxy = Proxy.Instance;
            Assert.That(Proxy.Instance.Storage.Config.Count > 0);
            //further connections:
        }

        [Test]
        public void FurtherInitializeProxy_Test()
        {
            //further connections:
            Proxy proxy = Proxy.Instance;
            //check whether there is a local storage file:
            Assert.That(Proxy.Instance.Storage.Config.Count > 0);
        }


        void callBack_ServerModifiedNotify(object sender, NL.ADA.Common.ServerModifiedEventArgs e)
        {
            Assert.That(e.Modified != null);
        }

        [Test]
        public void CheckFirstTime_Test()
        {
            Proxy proxy = Proxy.Instance;
            proxy.DeleteLocalConfigTemplateFile(
                System.Configuration.ConfigurationManager.AppSettings["applicationcode"].ToString() + "adaconfig.ada");
            proxy.Dispose();
            //now, assure that new configs are loaded from the server
            string s = ConfigurationManager.AppSettings["testKey"];
            Assert.That(s == "testValue");
        }

        [Test]
        public void GetLocalCaseNoConnection_Test()
        {
        }

        [Test]
        public void RefreshStorageNormal_Test()
        {

            string tempKey = DateTime.Now.Ticks.ToString();

            string s = ConfigurationManager.AppSettings["test"];
            AdminServiceGateway gateWay = new AdminServiceGateway();
            CustomConfig configPre = gateWay.GetConfigByKey(tempKey, 1, 1);
            Assert.That(configPre.ConfigId == 0);
            var result = gateWay.InsertConfig(0, 1, tempKey, tempKey, 1, String.Empty, 1);
            Thread.Sleep(5000);
            CustomConfig configAfter = gateWay.GetConfigByKey(tempKey, 1, 1);
            Assert.That(configAfter.ConfigId > 0);
            var value = ConfigurationManager.AppSettings[tempKey];
            Assert.That(!string.IsNullOrEmpty(value));
        }
        [Test]
        public void RefreshStorageGeneric_Test()
        {

            string tempKey = DateTime.Now.Ticks.ToString();
            AdminServiceGateway gateWay = new AdminServiceGateway();
            CustomConfig configPre = gateWay.GetConfigByKey(tempKey, 1, 1);
            Assert.That(configPre.ConfigId == 0);
            var result = gateWay.InsertConfig(0, 1, tempKey, "6-20-2001", 1, String.Empty, 1);
            Thread.Sleep(5000);
            CustomConfig configAfter = gateWay.GetConfigByKey(tempKey, 1, 1);
            Assert.That(configAfter.ConfigId > 0);
            var value = ConfigurationManager<string>.AppSettings[tempKey];
            Assert.That(value.ToString() == "6-20-2001");
        }

        [Test]
        public void FromConfigurationManager_Test()
        {
            string value = String.Empty;
            if (ConfigurationManager<object>.AppSettings != null)
            {
                value = ConfigurationManager<string>.AppSettings["newValue"];

            }
            Assert.That(!string.IsNullOrEmpty(value));
        }
    }
}
